﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class ServiceFeedBackAdd : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string feedbackid = Request["feedbackid"] ?? "";
            string content = Request["content"] ?? "";
            bool isgood = true;
            try
            {
                isgood = bool.Parse(Request["isgood"]);
            }
            catch
            {
                Response.Write("113");//无效的布尔参数 
                return;
            }
            string serviceid = Request["serviceid"] ?? "";
            string userid = Request["userid"] ?? "";
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, feedbackid, content, isgood, serviceid, userid))
            {
                Response.Write("112");//密钥不正确 
                return;
            }


            if (Request["feedbackid"]!=null&&Request["content"] != null && Request["isgood"] != null && Request["serviceid"] != null && Request["userid"] != null)
            {
                //不能重复提交 
                if (BLL.FeedBackBLL.Instance.GetDetail(feedbackid) != null)
                {
                    Response.Write("306");//评论已存在 
                }

                if (BLL.ServiceInfoBLL.Instance.GetDetail(serviceid) == null)
                {
                    Response.Write("300");//服务编号无效
                    return;
                }
                
                if (BLL.UserInfoBLL.Instance.GetDetail(userid) == null)
                {
                    Response.Write("301");//用户编号无效
                    return;
                }

                if (string.IsNullOrEmpty(content))
                {
                    Response.Write("308");//内容不能为空
                    return;
                }
                else
                {

                    if (BadWordsFilter.Instance.HasBadWord(content))
                    {
                        Response.Write("309");//内容中含有敏感字符
                        return;
                    }
                }



                Model.FeedBack feedback = new Model.FeedBack();
                feedback.FeedBackID = feedbackid;//Guid.NewGuid().ToString();
                feedback.Content = content;
                feedback.IsGood = isgood;
                feedback.ServiceID = serviceid;
                feedback.UserId = userid;


                //判断订单的状态


                BLL.FeedBackBLL.Instance.Add(feedback);

                //修改订单的状态 


                Response.Write("100"); //添加成功
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string feedbackid, string content, bool isgood, string serviceid, string userid)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + feedbackid + content + serviceid + userid + (isgood ? "good" : "bad")).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }

        }
    }
}